package method;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * 读取java每个类的内容
 */
public class ParseJavaMethod {
	/**
	 * 
	 * @param sourceDir:
	 * F:\\eclipse_java_demo_space\\northstar-test\\src\\main\\java\\com\\rootnet\\businLog
	 * @param source:
	 * com.rootnet.businLog.ReturnReceiverLogThread
	 * @return
	 */
	public List<String> getMethCode(String sourceDir, String source){
        JavaProjectBuilder builder =  new  JavaProjectBuilder();
        //源码目录
        builder.addSourceTree(new File(sourceDir));
        Collection<JavaClass> classes = builder.getClasses();
        System.out.println(classes);
        JavaClass cls = builder.getClassByName(source);
        List<JavaMethod> methods = cls.getMethods();
        //方法体源代码
        List<String> methodCodes = new ArrayList<>();
        for(JavaMethod method : methods){
        	methodCodes.add(method.getCodeBlock()); //方法体的源码
        }		
        return methodCodes;
	}
	
	/**
	 * 
	 * @param sourceDir:
	 * F:\\eclipse_java_demo_space\\northstar-test\\src\\main\\java\\com\\rootnet\\businLog
	 * @param source:
	 * com.rootnet.businLog.ReturnReceiverLogThread
	 * @return
	 */
	public  List<JavaMethod> getMethInfo(String sourceDir, String source){
        JavaProjectBuilder builder =  new  JavaProjectBuilder();
        //源码目录
        builder.addSourceTree(new File(sourceDir));
        Collection<JavaClass> classes = builder.getClasses();
        System.out.println(classes);
        JavaClass cls = builder.getClassByName(source);
        List<JavaMethod> methods = cls.getMethods();
        return methods;
	}
	
    public static void main(String[] args) {
    	ParseJavaMethod method = new ParseJavaMethod();
//    	String filePath = "E:\\java\\codeauditing\\uselibs\\appsvr\\com\\stock\\qfii\\commisionmgr\\CommisionManager.class";
//    	String names[] = filePath.split("_unzip");
//    	String sourceName = names[1].replaceAll("\\\\", "\\.");
//    	if(sourceName.startsWith(".")) {
//    		sourceName = sourceName.replaceFirst("\\.", "");
//    	}
    	String filePath = "E:\\java\\test codaudiing java";
    	String sourceName = "com.xckj.controller.run.project.RunnerWriteController";
    	List<String> methodCodes = method.getMethCode(filePath, sourceName);
        for(String methodCode : methodCodes){
            System.out.println(methodCode); //方法体的源码
        }
    }
}